package cn.edu.scau.cmi.zhengweibo.client;

import cn.edu.scau.cmi.zhengweibo.singleton.Chairman;
import cn.edu.scau.cmi.zhengweibo.singleton.Marshal;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class SingletonClient extends Application{
	public void start(Stage primaryStage) throws Exception {
		StringBuffer textlist = new StringBuffer();
		
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(30,10,10,10));
        pane.setHgap(20);
        pane.setVgap(20);
        
        Label label = new Label("创建主席名称:");
        TextField text = new TextField();
        Button button = new Button("单例创建");
        
        Label label_Singleton = new Label();
       
       button.setOnAction(event->{
    	    Chairman chairman = Chairman.getInstance(text.getText());
    	    String label_text = chairman.getName()+chairman.toString()+"\n";
    	    textlist.append(label_text);
    	    label_Singleton.setText(textlist.toString());
        });
       pane.getChildren().addAll(label,text,button,label_Singleton);
       
       StringBuffer textlist_Mult = new StringBuffer();
       Label label_Mult = new Label();
       Button button_Mult = new Button("多例模式创建");
       button_Mult.setOnAction(event->{
    	   Marshal marshal = Marshal.getInstance();
    	   if(null == marshal) {
    		   textlist_Mult.append("你已经创建了10个元帅，不能再创建了!\n");
    	   }else {
    		   String label_text_Mult = marshal.toString()+"\n";
    		   textlist_Mult.append(marshal.getName()+label_text_Mult);
    	   }
    	   label_Mult.setText(textlist_Mult.toString());
       });
       pane.getChildren().addAll(button_Mult,label_Mult);
       	
        Scene scene = new Scene(pane, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("SingletonClient");
        primaryStage.show();
	}
	public static void main(String[] args) {
		launch(args);
	}
}